package com.xunker.appointment.controller;

import com.xunker.appointment.exception.AppointmentException;
import com.xunker.appointment.result.Result;
import com.xunker.appointment.result.ResultCodeEnum;
import com.xunker.appointment.service.ApiService;
import com.xunker.appointment.service.HospitalService;
import com.xunker.appointment.utils.HttpRequestHelper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(tags = "医院管理接口")
@RestController
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private ApiService apiService;
    @PostMapping("/order/submitOrder")
    /**
     * 预订下单
     */
    public Result agreeAccountLendProject(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
            if (!HttpRequestHelper.isSignEquals(paramMap,apiService.getSignKey())) {
                throw new AppointmentException(ResultCodeEnum.SIGN_ERROR);
            }
            Map<String,Object> resultMap=hospitalService.submitOrder(paramMap);
            return Result.ok(resultMap);
        }catch (AppointmentException e){
            return Result.fail().message(e.getMessage());
        }
    }

    /**
     * 更新支付状态
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/order/updatePayStatus")
    public Result updatePayStatus(HttpServletRequest request,HttpServletResponse response){
        try {
            Map<String,Object> paramMap=HttpRequestHelper.switchMap(request.getParameterMap());
            if (!HttpRequestHelper.isSignEquals(paramMap,apiService.getSignKey())) {
                throw new AppointmentException(ResultCodeEnum.SIGN_ERROR);
            }
            hospitalService.updatePayStatus(paramMap);
            return Result.ok();
        } catch (AppointmentException e) {
            return Result.fail().message(e.getMessage());
        }
    }

    /**
     * 更新取消预约状态
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/order/updateCancelStatus")
    public Result updateCancelStatus(HttpServletRequest request,HttpServletResponse response){
        try {
            Map<String,Object> paramMap=HttpRequestHelper.switchMap(request.getParameterMap());
            if (!HttpRequestHelper.isSignEquals(paramMap,apiService.getSignKey())) {
                throw new AppointmentException(ResultCodeEnum.SIGN_ERROR);
            }
            hospitalService.updateCancelStatus(paramMap);
            return Result.ok();
        } catch (AppointmentException e) {
            return Result.fail().message(e.getMessage());
        }
    }
}
